package uiLayer;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import errorLayer.*;
import controlLayer.StaffCtrl;
import modelLayer.Staff;

public class StaffGUI extends JPanel {
	
	private JTextField staffIDField = new JTextField();
	private JTextField staffNameField = new JTextField();
	private JTextField staffCPRField = new JTextField();
	private JTextField staffEMAILField = new JTextField();
	private JComboBox allStaffBox = new JComboBox();
	private List<Staff> allStaff = new ArrayList<>();
	private StaffCtrl sc = new StaffCtrl();
	private JButton addBtn = new JButton("Add");
	private JButton editBtn = new JButton("Edit");
	private JButton removeBtn = new JButton("Remove");
	
	public StaffGUI () {
		
		setLayout(new BorderLayout(0, 0));
		
		allStaff = sc.allStaff();
		
		setup();
		
	}
	
	private void setup() {
		
		JPanel staffContent = new JPanel();
		staffContent.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLoweredBevelBorder(), "Staff"));
		staffContent.setLayout(new BorderLayout(0, 0));
		add(staffContent, BorderLayout.NORTH);
		
		JPanel textNameArea = new JPanel();
		textNameArea.setLayout(new GridLayout(4, 1, 0, 5));
		staffContent.add(textNameArea, BorderLayout.WEST);
		
		JPanel textArea = new JPanel();
		textArea.setBorder(new EmptyBorder(5, 5, 5, 5));
		textArea.setLayout(new GridLayout(4, 1, 0, 5));
		staffContent.add(textArea, BorderLayout.CENTER);
		
		JPanel buttonArea = new JPanel();
		buttonArea.setBorder(new EmptyBorder(5, 5, 5, 5));
		buttonArea.setLayout(new GridLayout(4, 1, 0, 5));
		staffContent.add(buttonArea, BorderLayout.EAST);
		
		staffIDField.setEditable(false);
		
		allStaffBox.addItem("New staff");
		
		if (allStaff.size() == 0) {
			
			allStaffBox.addItem("No staff");
			
		}
		
		for (int i = 0; i < allStaff.size(); ++i) {
			
			allStaffBox.addItem(allStaff.get(i));
			
		}
		
		allStaffBox.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				
				if (e.getItem() == "New staff") {
					
					addBtn.setEnabled(true);
					editBtn.setEnabled(false);
					removeBtn.setEnabled(false);
					
					staffIDField.setText("");
					staffNameField.setText("");
					staffCPRField.setText("");
					staffEMAILField.setText("");
					
				}
				else if (e.getItem() != "New staff") {
					
					addBtn.setEnabled(false);
					editBtn.setEnabled(true);
					removeBtn.setEnabled(true);
					
					try {
						
						Staff staff = (Staff) e.getItem();
						
						staffIDField.setText("" + staff.getID());
						staffNameField.setText(staff.getName());
						staffCPRField.setText("" + staff.getCprNo());
						staffEMAILField.setText(staff.getEmail());
						
					}
					catch (Exception ex) {
						System.out.println("Exception: " + ex);
					}
					
				}
				
			}
		});
		
		buttonArea.add(allStaffBox);
		
		addBtn.setPreferredSize(new Dimension(150, 20));
		addBtn.setMinimumSize(new Dimension(150, 20));
		addBtn.setMaximumSize(new Dimension(150, 20));
		
		addBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				
				Staff staff = new Staff();
				
				if (!staffNameField.getText().isEmpty() && !staffCPRField.getText().isEmpty() && !staffEMAILField.getText().isEmpty()) {
					
					staff.setName(staffNameField.getText());
					staff.setCprNo(Integer.parseInt(staffCPRField.getText()));
					staff.setEmail(staffEMAILField.getText());
					
					sc.addStaff(staff);
					
					staff.setID(sc.highestID());
					
					allStaffBox.addItem(staff);
					allStaffBox.setSelectedIndex((allStaffBox.getItemCount() - 1));
					
				}
				else {
					
					if (staffNameField.getText().isEmpty()) {
						new EmptyField("Staff name");
					}
					
					if (staffCPRField.getText().isEmpty()) {
						new EmptyField("Staff CPR");
					}
					
					if (staffEMAILField.getText().isEmpty()) {
						new EmptyField("Staff e-mail");
					}
				}
				
			}
		});
		
		buttonArea.add(addBtn);
		
		editBtn.setPreferredSize(new Dimension(150, 20));
		editBtn.setMinimumSize(new Dimension(150, 20));
		editBtn.setMaximumSize(new Dimension(150, 20));
		editBtn.setEnabled(false);
		
		editBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				
				Staff staff = new Staff();
				
				if (!staffIDField.getText().isEmpty()) {
					
					staff.setID(Integer.parseInt(staffIDField.getText()));
					staff.setName(staffNameField.getText());
					staff.setCprNo(Integer.parseInt(staffCPRField.getText()));
					staff.setEmail(staffEMAILField.getText());
					
					sc.updateStaff(staff);
					
					allStaffBox.removeItemAt(allStaffBox.getSelectedIndex());
					allStaffBox.addItem(staff);
					allStaffBox.setSelectedItem((allStaffBox.getItemCount() - 1));
					
				}
				
			}
		});
		
		buttonArea.add(editBtn);
		
		removeBtn.setPreferredSize(new Dimension(150, 20));
		removeBtn.setMinimumSize(new Dimension(150, 20));
		removeBtn.setMaximumSize(new Dimension(150, 20));
		removeBtn.setEnabled(false);
		
		removeBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				
				String deletion = "Staff: " + staffIDField.getText() + ", " + staffNameField.getText();
				
				DeleteWarning dw = new DeleteWarning(deletion);
				
				if (dw.getAnswer()) {
					sc.removeStaff(Integer.parseInt(staffIDField.getText()));
				
					allStaffBox.removeItemAt(allStaffBox.getSelectedIndex());
				}
				
			}
		});
		
		buttonArea.add(removeBtn);
		
		textNameArea.add(new JLabel("ID"));
		textNameArea.add(new JLabel("Name"));
		textNameArea.add(new JLabel("CPR.No."));
		textNameArea.add(new JLabel("E-Mail"));
		
		textArea.add(staffIDField);
		textArea.add(staffNameField);
		textArea.add(staffCPRField);
		textArea.add(staffEMAILField);
		
	}

}
